package com.jdzy.student.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Component
public class FildMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        this.setFieldValByName("version",1L,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
    }

    /*
            Date now = new Date();

        // 将Date转换为Instant
        Instant instant = now.toInstant();

        // 考虑时区，例如使用系统默认时区
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());

        // 使用DateTimeFormatter进行格式化，但这里直接使用ZonedDateTime的toString方法默认输出接近所需格式
        String formattedDateTime = zonedDateTime.toString();

        // 输出格式化后的时间
        System.out.println(formattedDateTime);	//2024-05-27T17:44:33.123+08:00[Asia/Shanghai]


        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedWithPrecision = zonedDateTime.format(formatter);
        System.out.println(formattedWithPrecision);	//2024-05-27 17:44:56
     */
}
